//
//  FiltrateFirstView.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/11/28.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "FiltrateFirstView.h"

@implementation FiltrateFirstView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configure];
    }
    return self;
}

- (instancetype)initWithType:(FiltrateType)type {
    self = [super init];
    if (self) {
        _type = type;
        [self configure];
    }
    return self;
}

- (void)configure {
    self.view = [[[NSBundle mainBundle] loadNibNamed:@"FiltrateFirstView" owner:self options:nil] firstObject];
    self.view.frame = self.bounds;
    [self addSubview:self.view];
    
    self.firstLabel.font = TextPingFangFontSize(14);
    self.secondLabel.font = TextPingFangFontSize(14);
    self.thirdLabel.font = TextPingFangFontSize(14);
    self.fourthLabel.font = TextPingFangFontSize(14);
    
    self.firstLabel.textColor = [UIColor gray66Color];
    self.secondLabel.textColor = [UIColor gray66Color];
    self.thirdLabel.textColor = [UIColor gray66Color];
    self.fourthLabel.textColor = [UIColor gray66Color];
    
    self.firstLabel.text = @"推荐";
    self.secondLabel.text = @"销量";
    self.thirdLabel.text = @"价格";
    self.fourthLabel.text = @"筛选";
    
    [self changePriceSortArrow:0];
    
    switch (self.type) {
        case FiltrateTypeThree:
        {
            [self.fourthView setHidden:YES];
            self.fourthViewWLayout.constant = 0;
        } break;
        case FiltrateTypeFour:
        {
            [self.fourthView setHidden:NO];
            self.fourthViewWLayout.constant = ScreenWidth/4.0;
        } break;
        default:
            break;
    }
}

- (void)buttonClick:(NSInteger)index {
    self.currentIndex = index;
    if (index == 0) {
        self.firstLabel.textColor = [UIColor redF2color];
        self.secondLabel.textColor = [UIColor gray66Color];
        self.thirdLabel.textColor = [UIColor gray66Color];
        self.fourthLabel.textColor = [UIColor gray66Color];
        [self changePriceSortArrow:0];
    } else if (index == 1) {
        self.firstLabel.textColor = [UIColor gray66Color];
        self.secondLabel.textColor = [UIColor redF2color];
        self.thirdLabel.textColor = [UIColor gray66Color];
        self.fourthLabel.textColor = [UIColor gray66Color];
        [self changePriceSortArrow:0];
    } else if (index == 2) {
        self.firstLabel.textColor = [UIColor gray66Color];
        self.secondLabel.textColor = [UIColor gray66Color];
        self.thirdLabel.textColor = [UIColor redF2color];
        self.fourthLabel.textColor = [UIColor gray66Color];
        if (self.arrowStatus == 1) {
            [self changePriceSortArrow:2];
        } else {
            [self changePriceSortArrow:1];
        }
    } else if (index == 3) {
        self.firstLabel.textColor = [UIColor gray66Color];
        self.secondLabel.textColor = [UIColor gray66Color];
        self.thirdLabel.textColor = [UIColor gray66Color];
        self.fourthLabel.textColor = [UIColor gray66Color];
        [self changePriceSortArrow:0];
    }
    if (self.block) {
        if (self.arrowStatus == 0) {
            self.block(self.currentIndex);
        } else if (self.arrowStatus == 1) {
            self.block(4);
        } else {
            self.block(5);
        }
    }
}

- (void)changePriceSortArrow:(NSInteger)status {
    self.arrowStatus = status;
    if (status == 0) {
        self.arrowImageView.image = [UIImage imageNamed:@"wu"];
    } else if (status == 1) {
        self.arrowImageView.image = [UIImage imageNamed:@"topup"];
    } else {
        self.arrowImageView.image = [UIImage imageNamed:@"bottom"];
    }
}

#pragma mark - Action

- (IBAction)firstButtonClick:(id)sender {
    [self buttonClick:0];
//    if (self.block) {
//        self.block(0);
//    }
}

- (IBAction)secondButtonClick:(id)sender {
    [self buttonClick:1];
//    if (self.block) {
//        self.block(1);
//    }
}

- (IBAction)thirdButtonClick:(id)sender {
    [self buttonClick:2];
//    if (self.block) {
//        self.block(2);
//    }
}

- (IBAction)fourthButtonClick:(id)sender {
    [self buttonClick:3];
//    if (self.block) {
//        self.block(3);
//    }
}
@end
